def solution(s: str, k: int) -> str:
    # write code here
    res = ""
    if k <= 0:
        return s
    for c in s:
        if c == 'a':
            res += "bc"
        if c == 'b':
            res += 'ca'
        if c == 'c':
            res += 'ab'
    k -= 1

    return solution(res, k)


def solution1(s: str, k: int) -> str:
    if k <= 0:
        return s

    # 使用字典存储映射规则
    mapping = {'a': 'bc', 'b': 'ca', 'c': 'ab'}

    # 使用列表收集字符提高效率
    res = []
    for c in s:
        res.append(mapping[c])

    # 递归调用
    return solution(''.join(res), k - 1)


if __name__ == '__main__':
    print(solution("abc", 2) == 'caababbcbcca')
    print(solution("abca", 3) == 'abbcbccabccacaabcaababbcabbcbcca')
    print(solution("cba", 1) == 'abcabc')
